package com.wzgl.core.webmagic.utils;

import java.util.List;

import org.apache.log4j.Logger;

import com.alibaba.druid.filter.stat.StatFilter;
import com.jfinal.core.JFinal;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.IDataSourceProvider;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.druid.DruidPlugin;
import com.wzgl.core.base.JModelMapping;
import com.wzgl.core.base.Table;
import com.wzgl.core.utils.ClassUtils;
import com.wzgl.core.utils.StringUtils;

import redis.clients.jedis.JedisPool;

public class DruidPluginUtils {
	static boolean is_start = false;
	private final Logger LOGGER = Logger.getLogger(RedisUtil.class);  
	public DruidPluginUtils() { 
    	DruidPlugin druidPlugin = DruidPluginUtils.createDruidPlugin();
    	ActiveRecordPlugin arp =DruidPluginUtils.createRecordPlugin(druidPlugin);
		arp.setShowSql(JFinal.me().getConstants().getDevMode());
		arp.setShowSql(true);
		
		druidPlugin.start();
		arp.start();
		is_start = true;
    }
    
    public static boolean isStart(){
    	return is_start;
    }
	//配置数据连接
	public static DruidPlugin createDruidPlugin() {

		Prop dbProp = PropKit.use("db.properties");
		String db_host = dbProp.get("db_host").trim();

		String db_host_port = dbProp.get("db_host_port");
		db_host_port = StringUtils.isNotBlank(db_host_port) ? db_host_port.trim() : "3306";

		String db_name = dbProp.get("db_name").trim();
		String db_user = dbProp.get("db_user").trim();
		String db_password = dbProp.get("db_password").trim();

		String jdbc_url = "jdbc:mysql://" + db_host + ":" + db_host_port + "/" + db_name + "?" + "useUnicode=true&"
				+ "characterEncoding=utf8&" + "zeroDateTimeBehavior=convertToNull";

		DruidPlugin druidPlugin = new DruidPlugin(jdbc_url, db_user, db_password);
		druidPlugin.addFilter(new StatFilter());
		
		return druidPlugin;
	}
  //配置ActiveRecordPlugin 并自动装载表 表明必须为小写
  	@SuppressWarnings({ "rawtypes", "unchecked" })
  	public static ActiveRecordPlugin createRecordPlugin(IDataSourceProvider dsp) {
  		ActiveRecordPlugin arPlugin = new ActiveRecordPlugin(dsp);
  		List<Class<Model>> modelClassList = ClassUtils.scanSubClass(Model.class);
  		if (modelClassList != null) {
  			String tablePrefix = PropKit.use("db.properties").get("db_tablePrefix");
  			tablePrefix = (StrKit.isBlank(tablePrefix)) ? "" : (tablePrefix.trim());
  			for (Class<?> clazz : modelClassList) {
  				Table tb = clazz.getAnnotation(Table.class);
  				if (tb == null)
  					continue;
  				String tname = tablePrefix + tb.tableName();
  				if (StringUtils.isNotBlank(tb.primaryKey())) {
  					arPlugin.addMapping(tname, tb.primaryKey(), (Class<? extends Model<?>>) clazz);
  				} else {
  					arPlugin.addMapping(tname, (Class<? extends Model<?>>) clazz);
  				}

  				JModelMapping.me().mapping(clazz.getSimpleName().toLowerCase(), tname);
  			}
  		}
  		return arPlugin;
  	}
}
